package com.guangwei.wechatautoanswer

import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.android.synthetic.main.activity_name_list_maintain.*
import java.io.BufferedReader
import java.io.InputStreamReader
import android.accessibilityservice.GestureDescription
import android.R.attr.y
import android.R.attr.x
import android.R.attr.top
import android.R.attr.left
import android.content.ContentValues.TAG
import android.graphics.Path
import android.graphics.Point
import android.graphics.Rect
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.util.Log


class HelpService : AccessibilityService() {
    val TAG = "AsHelpServise"
    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        Log.i(TAG, "onAccessibilityEvent++")
        //获取当前界面的布局信息
        val nodeInfo: AccessibilityNodeInfo = when {
            rootInActiveWindow != null -> rootInActiveWindow
            event.source != null -> event.source
            else -> return
        }
        println("get AccessibilityEvent")

        // 微信接听操作
        //获取控件  并根据内容执行相应的操作
        //var listNodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/z6")
        val listNodes = nodeInfo.findAccessibilityNodeInfosByText("邀请你进行视频通话")
        //如果id为eg7的控件存在
        if (listNodes.size>0){
            Log.i(TAG, "Find the node of wechart which want to connect")
            //判断这个控件的内容
            for (node in listNodes) {
                // 点击接听按钮的坐标，但不是每个机型的坐标都一样，需要根据机型和微信版本调整
                answerWechat(Rect(670, 1890, 670+194, 1890+194))
            }
        }
    }

    // 点击接听按钮的坐标，但不是每个机型的坐标都一样，需要根据机型和微信版本调整
    private fun answerWechat(rect: Rect) {
        Log.d(TAG, "printTree: bound:$rect")
        val position = Point(rect.left + 10, rect.top + 10)
        val builder = GestureDescription.Builder()
        val p = Path()
        p.moveTo(position.x.toFloat(), position.y.toFloat())
        builder.addStroke(GestureDescription.StrokeDescription(p, 0L, 100L))
        val gesture = builder.build()
        val isDispatched =
            dispatchGesture(gesture, object : AccessibilityService.GestureResultCallback() {
                override fun onCompleted(gestureDescription: GestureDescription) {
                    super.onCompleted(gestureDescription)
                    Log.d(TAG, "onCompleted: 完成..........")
                }

                override fun onCancelled(gestureDescription: GestureDescription) {
                    super.onCancelled(gestureDescription)
                    Log.d(TAG, "onCompleted: 取消..........")
                }
            }, null)
    }

    private fun isNameInList(name: String): Boolean {
        try {
            val fileInputStream = openFileInput("data.txt")

            //把文件内容读取进缓冲读取器（use方法会自动对BufferedReader进行关闭）
            BufferedReader(InputStreamReader(fileInputStream)).use {
                var line: String
                while (true) {
                    line = it.readLine() ?: break //当有内容时读取一行数据，否则退出循环
                    if (name == line)
                        return true
                }
            }
        } catch (throwable: Throwable) {
            // 捕获到异常的处理
        }

        return false

    }


    override fun onInterrupt() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}